﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Model;
using BLL;
using Utility;
using System.IO;

public partial class VehicleUI_Kilometre_KiloMetreAMV : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            if (Request.QueryString["Type"] == null)
            {
                if (Session["FilterFrameNO"] != null)
                {
                    txt_FrameNO.Text = Session["FilterFrameNO"].ToString();
                }
            }
            else if (Request.QueryString["Type"].ToString() == "View")
            {
                LoadService();
                txt_VehicleID.Enabled = false;
                txt_KiloDate.Enabled = false;
                txt_FrameNO.Enabled = false;
                txt_Count.Enabled = false;
                ful_GPS.Visible = false;
                btn_Sure.Visible = false;
            }
            else if (Request.QueryString["Type"].ToString() == "Edit")
            {
                LoadService();
            }
        }
    }

    protected void LoadService()
    {
        int _id = Convert.ToInt32(Request.QueryString["ID"]);
        Kilometre cs = new KilometreBLL().GetModel(_id);
        if (cs != null)
        {
            txt_VehicleID.Text = cs.VehicleID;
            txt_FrameNO.Text = cs.FrameNO;
            txt_KiloDate.Text = cs.KM_Time.ToString();
            txt_Count.Text = cs.KM_Count.ToString();
            if (cs.KM_Image != string.Empty)
            {
                img_GPS.ImageUrl = GlobalArgs.KilometrePath + cs.KM_Image;
                img_GPS.DataBind();
            }
        }
    }


    protected void btn_Sure_Click(object sender, EventArgs e)
    {
        string _frameNO = txt_FrameNO.Text.Trim();
        bool reExist = new VehicleBLL().Exists(_frameNO);
        if (!reExist)
        {
            UtilityService.Alert(this.Page, "不存在该车辆信息,请重新录入车架号!");
            txt_FrameNO.Focus();
            return;
        }

        if (!ValidateService.IsNumber(txt_Count.Text.Trim()))
        {
            UtilityService.Alert(this.Page, "格式错误!");
            txt_Count.Focus();
            return;
        }

        if (Request.QueryString["Type"] == null)
        {
            Kilometre cs = new Kilometre();

            cs.FrameNO = txt_FrameNO.Text.Trim();
            cs.KM_Time = DateTime.Parse(txt_KiloDate.Text.Trim());
            cs.KM_Count = int.Parse(txt_Count.Text.Trim());
            cs.OrganID = (int)Session["OrganID"];
            cs.InputBy = Session["UserID"].ToString();

            string f1 = UploadFile(ful_GPS, 0);

            cs.KM_Image = f1.Length > 5 ? f1 : "";

            int re = new KilometreBLL().Add(cs);

            if (re > 0)
            {
                string msg = "";
                if (f1 == "err")
                {
                    msg += "GPS图片上传失败!";
                }

                UtilityService.AlertAndRedirect(this.Page, "添加成功! " + msg, "KilometreMgr.aspx");
            }
            else
            {
                FileInfo file1 = new FileInfo(Server.MapPath(GlobalArgs.KilometrePath) + f1);
                if (file1.Exists)
                {
                    file1.Delete();
                }
                UtilityService.Alert(this.Page, "添加失败! ");
            }
        }
        else if (Request.QueryString["Type"] == "Edit")
        {
            int _id = Convert.ToInt32(Request.QueryString["ID"]);
            Kilometre cs = new KilometreBLL().GetModel(_id);
            cs.FrameNO = txt_FrameNO.Text.Trim();
            cs.KM_Time = DateTime.Parse(txt_KiloDate.Text.Trim());
            cs.KM_Count = int.Parse(txt_Count.Text.Trim());

            string f1 = "";
            if (ful_GPS.HasFile)
            {
                f1 = UploadFile(ful_GPS, 0);
                cs.KM_Image = f1.Length > 5 ? f1 : "";
            }

            bool re = new KilometreBLL().Update(cs);

            if (re)
            {
                string msg = "";
                if (f1 == "err")
                {
                    msg += "图片上传失败!";
                }
                UtilityService.AlertAndRedirect(this.Page, "修改成功! " + msg, "KilometreMgr.aspx");
            }
            else
            {
                FileInfo file1 = new FileInfo(Server.MapPath(GlobalArgs.KilometrePath) + f1);
                if (file1.Exists)
                {
                    file1.Delete();
                }

                UtilityService.Alert(this.Page, "修改失败! ");
            }
        }
    }

    public string UploadFile(FileUpload ful, int type)
    {

        try
        {
            if (!ful.HasFile)
            {
                // this.Upload_info.Text = "请选择上传文件！";
                return "none";
            }
            else
            {
                string filepath = ful.PostedFile.FileName;  //得到的是文件的完整路径,包括文件名，如：C:\My Pictures\20022775_m.jpg 
                string filename = "";
                string ext = filepath.Substring(filepath.LastIndexOf("."));
                filename = Session["OrganID"].ToString() + "_" + DateTime.Now.Ticks.ToString() + "_kilo" + ext;
                //if (type == 0)
                //{

                //    filename = Session["OrganID"].ToString() + "_" + DateTime.Now.Ticks.ToString() + "_gps" + ext;//取得文件在服务器上保存的位置C:\Inetpub\20022775_m.jpg
                //}
                //else
                //{
                //    filename = Session["OrganID"].ToString() + "_" + DateTime.Now.Ticks.ToString() + "_chk" + ext;
                //}

                string serverpath = Server.MapPath(GlobalArgs.KilometrePath) + filename;//取得文件在服务器上保存的位置C:\Inetpub\wwwroot\WebSite1\images\20022775_m.jpg

                ful.PostedFile.SaveAs(serverpath);//将上传的文件另存为 
                //this.Upload_info.Text = "上传成功！";
                return filename;
            }
        }
        catch (Exception ex)
        {
            UtilityService.Alert(this.Page, "上传发生错误!原因是:" + ex.ToString());
            //this.Upload_info.Text = "上传发生错误！原因是：" + ex.ToString();
            return "err";
        }

    }

    protected void btn_Sel1_Click(object sender, EventArgs e)
    {

    }
    protected void btn_Sel_Click(object sender, EventArgs e)
    {

    }
    protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
    {
        UtilityService.Open(this.Page, "../../ImageView.aspx?url=" + ((ImageButton)sender).ImageUrl);
    }
    protected void btn_Back_Click(object sender, EventArgs e)
    {
        Response.Redirect("KiloMetreMgr.aspx");
    }
}
